dojo.provide("velcro.orgunit");
dojo.require("lotex.io");

velcro.orgunit.getOrgId = function(orgString/*'/白海豚国际酒店/总经理办公室'*/) {
    var orgArray = orgString.split('/');
    if(lotex.string.isBlank(orgArray[0])) orgArray.shift();

d("velcro.orgunit.getOrgId",orgArray);
    var orgRoot = orgArray[0];
    var orgRootSql = "SELECT id FROM orgunit WHERE (objname = '"
        + orgRoot + "')";
d("orgRootSql",orgRootSql);
    var orgRootId = velcro.dao.find(orgRootSql);
d("orgRootId",orgRootId);

    if(orgArray.length==1) return orgRootId;
    var orgPid = orgRootId;
    var orgId;
    for(var i=1;i<orgArray.length;i++) {
        var orgName = orgArray[i];
        var sql = "SELECT orgunitlink.oid"
            + " FROM orgunitlink INNER JOIN"
            + " orgunit ON orgunitlink.oid = orgunit.id"
            + " WHERE (orgunitlink.pid = '"+orgPid+"') AND"
            + " (orgunit.objname = '"+orgName+"')";
d("sql : ",sql);
        orgId = velcro.dao.find(sql);
        orgPid = orgId;
    }
    return orgId;
}
